# -*- coding: utf-8 -*- 
"""
@Author : twl
@Date : 2022/7/19 11:04
File Description:

"""
import sys

from flask import Flask
from base.constant.nacos_constants import *
from base.util.ip_util import IpUtil
from common.boot_args import config_name, port
from common.logger import LoggerHandler
from config import profiles

logging = LoggerHandler(__name__)


def create_app():
    from v1.health_api import health
    from v1.sleep_wake_api import sleepWake
    from v1.recog_mobile_api import recogMobile
    from v1.recog_mobile_final import recogMobileFinal
    from v1.sleep_wake_final import sleepWakeFinal
    from V2.sleep_api import sleep
    from V2.wake_api import wake
    from V2.recog_mob_api import recMob
    new_app = Flask(__name__)
    logging.info('当前配置：' + config_name)
    new_app.config.from_object(profiles.app_config[config_name])
    new_app.register_blueprint(health, url_prefix='/analyze/')
    new_app.register_blueprint(sleepWake, url_prefix='/analyze/sleepWake')
    new_app.register_blueprint(recogMobile, url_prefix='/analyze/recogMobile')
    new_app.register_blueprint(recogMobileFinal, url_prefix='/analyze/recogMobileFinal')
    new_app.register_blueprint(sleepWakeFinal, url_prefix='/analyze/sleepWakeFinal')

    new_app.register_blueprint(sleep, url_prefix='/analyze/sleep')
    new_app.register_blueprint(wake, url_prefix='/analyze/wake')
    new_app.register_blueprint(recMob, url_prefix='/analyze/recMob')
    return new_app


def init_nacos(new_app, final_port):
    from common import global_val as gol
    gol.init()
    server_id = IpUtil.get_host_ip()
    gol.set_value(SERVER_ID_KEY, server_id)
    gol.set_value(SERVER_PORT_KEY, final_port)


app = create_app()

if __name__ == '__main__':
    final_port = port if port is not None else 8668
    init_nacos(app, final_port)
    app.run(host="0.0.0.0", port=final_port, use_reloader=False)
